<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\func\Lib;
use app\common\model\GiftApply;
use app\common\model\GiftCount;
use app\common\model\GiftLog;
use think\Db;

/**
 * 手机短信接口
 */
class Gift extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function bindGift(){
        $code = $this->request->post('code');
        $applyId = $this->request->post('apply_id');//客户申领设备ID

        $gift = Lib::getGiftByCode($code);
        $productApply = Lib::getApply($applyId);
        if(!$gift){
            $this->error("礼品不存在");
        }
        if(!$productApply){
            $this->error("申请记录不存在");
        }

        $saleStock = Lib::getSaleGiftCount($this->auth->admin_id,$gift->id);
        if(!$saleStock || ($saleStock->stock_number - $saleStock->use_number) <= 0){
            $this->error("可用数量不足");
        }

        $msg = "";
        $result = false;
        Db::startTrans();
        try {
            $log = new GiftLog();
            $log->admin_id = $this->auth->admin_id;
            $log->gift_id = $gift->id;
            $log->apply_id = $productApply->id;
            $log->user_id = $productApply->user_id;
            $log->save();

            $saleStock->use_number += 1;
            $saleStock->save();

            $productApply->status = 5;
            $productApply->save();

            $result = true;
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            $msg = $e->getMessage();
        }

        if($result){
            $this->success("操作成功");
        }else{
            $this->error("操作失败:".$msg);
        }
    }

    public function checkApply(){
        $applyId = $this->request->post('apply_id');//业务员申领礼品ID
        $type = $this->request->post('type');

        $apply = Lib::getGiftApplyById($applyId);
        if(!$apply){
            $this->error("申请不存在");
        }

        $gift = Lib::getGiftById($apply->gift_id);
        if($gift->stock < $apply->number){
            $this->error("库存不足");
        }

        $msg = "";
        $result = false;
        Db::startTrans();
        try {
            if($type == 2){
                $apply->status = 2;
                $apply->save();

                $gift->stock -= $apply->number;
                $gift->save();

                $saleStock = Lib::getSaleGiftCount($apply->admin_id,$apply->gift_id);
                if(!$saleStock){
                    $saleStock = new GiftCount();
                    $saleStock->admin_id = $apply->admin_id;
                    $saleStock->gift_id = $apply->gift_id;
                    $saleStock->stock_number = $apply->number;
                }else{
                    $saleStock->stock_number += $apply->number;
                }
                $saleStock->save();
            }else{
                $apply->status = 4;
                $apply->save();
            }

            $result = true;
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            $msg = $e->getMessage();
        }

        if($result){
            $this->success("操作成功");
        }else{
            $this->error("操作失败:".$msg);
        }
    }

    public function getGiftApplyList(){
        $this->success("申请列表", Lib::getGiftApplyList($this->auth->id));
    }

    public function saveApply(){
        $giftId = $this->request->post('gift_id');
        $number = $this->request->post('number');

        $gift = Lib::getGiftById($giftId);
        if(!$gift){
            $this->error("礼品不存在");
        }
        if($number == 0 || $number > $gift->stock){
            $this->error("请确认申请数量");
        }

        $apply = new GiftApply();
        $apply->admin_id = $this->auth->admin_id;
        $apply->gift_id = $giftId;
        $apply->code = $gift->code;
        $apply->number = $number;
        $result = $apply->save();

        if($result){
            $this->success("操作成功");
        }else{
            $this->error("操作失败");
        }
    }
}
